/*
 *
 *    Copyright (C) 2011 Freddy Barrera All Rights Reserved.
 *
 *    This program is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU General Public License
 *    as published by the Free Software Foundation; either version 3
 *    of the License, or Mozilla Public License any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
 *    USA.
 *
 *    Redistribution and use in source and binary forms, with or without
 *    modification, are permitted provided that the following conditions are
 *    met:
 *
 *    -Redistribution of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *    -Redistribution in binary form must reproduce the above copyright notice,
 *     this list of conditions and the following disclaimer in the documentation
 *     and/or other materials provided with the distribution.
 *
 *    Neither the name of Freddy Barrera or the names of contributors may
 *    be used to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 *    This software is provided "AS IS," without a warranty of any kind. ALL
 *    EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
 *    ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
 *    OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. Freddy Barrera
 *    AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 *    AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
 *    DERIVATIVES. IN NO EVENT WILL Freddy Barrera OR ITS LICENSORS BE LIABLE
 *    FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
 *    CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
 *    REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
 *    INABILITY TO USE THIS SOFTWARE, EVEN IF Freddy HAS BEEN ADVISED OF THE
 *    POSSIBILITY OF SUCH DAMAGES.
 *
 *    You acknowledge that this software is not designed, licensed or intended
 *    for use in the design, construction, operation or maintenance of any
 *    nuclear facility.
 *
 *
 */

package mx.uacam.ermilo.ercu.data.dao.hibernate;

import mx.uacam.ermilo.ercu.data.HibernateUtil;
import mx.uacam.ermilo.ercu.data.dao.DAOFactory;
import mx.uacam.ermilo.ercu.data.dao.interfaces.AnexosDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.CurriculumsDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.EmpleadosDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.EstadosDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.ExperienciasLaboralesDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.FormacionesAcademicasDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.InstitucionesDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.LugaresNacimientoDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.ModulosDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.MunicipiosDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.NivelesDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.OrganizacionesDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.PermisosDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.RolesDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.TiposAnexosDAO;
import mx.uacam.ermilo.ercu.data.dao.interfaces.UsuariosDAO;
import org.hibernate.Session;

/**
 *
 * @author Freddy Barrera
 */
public class HibernateDAOFactory extends DAOFactory {

    /**
     * This method create a instance of DAOClass.
     * @param daoClass the class tocreate at instance.
     * @return the instance of DAOClass.
     */
    private GenericHibernateDAO instantiateDAO(final Class daoClass) {
        try {
            GenericHibernateDAO dao =
                    (GenericHibernateDAO) daoClass.newInstance();
            dao.setSession(getCurrentSession());
            return dao;
        } catch (Exception ex) {
            throw new RuntimeException("Can not instantiate DAO: "
                    + daoClass, ex);
        }
    }

    /**
     * This method return the hibernate session.
     * @return the hibernate session.
     */
    protected final Session getCurrentSession() {
        return HibernateUtil.getSessionFactory().openSession();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final AnexosDAO getAnexosDAO() {
        return (AnexosDAO) instantiateDAO(AnexosDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final CurriculumsDAO getCurriculumsDAO() {
        return (CurriculumsDAO) instantiateDAO(CurriculumsDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final EmpleadosDAO getEmpleadosDAO() {
        return (EmpleadosDAO) instantiateDAO(EmpleadosDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final EstadosDAO getEstadosDAO() {
        return (EstadosDAO) instantiateDAO(EstadosDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final ExperienciasLaboralesDAO getExperienciasLaboralesDAO() {
        return (ExperienciasLaboralesDAO) instantiateDAO(
                ExperienciasLaboralesDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final FormacionesAcademicasDAO getFormacionesAcademicasDAO() {
        return (FormacionesAcademicasDAO) instantiateDAO(
                FormacionesAcademicasDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final InstitucionesDAO getInstitucionesDAO() {
        return (InstitucionesDAO) instantiateDAO(InstitucionesDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final LugaresNacimientoDAO getLugaresNacimientoDAO() {
        return (LugaresNacimientoDAO) instantiateDAO(LugaresNacimientoDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final ModulosDAO getModulosDAO() {
        return (ModulosDAO) instantiateDAO(ModulosDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final MunicipiosDAO getMunicipiosDAO() {
        return (MunicipiosDAO) instantiateDAO(MunicipiosDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final NivelesDAO getNivelesDAO() {
        return (NivelesDAO) instantiateDAO(NivelesDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final OrganizacionesDAO getOrganizacionesDAO() {
        return (OrganizacionesDAO) instantiateDAO(OrganizacionesDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final PermisosDAO getPermisosDAO() {
        return (PermisosDAO) instantiateDAO(PermisosDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final RolesDAO getRolesDAO() {
        return (RolesDAO) instantiateDAO(RolesDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final TiposAnexosDAO getTiposAnexosDAO() {
        return (TiposAnexosDAO) instantiateDAO(TiposAnexosDAOHibernate.class);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public final UsuariosDAO getUsuariosDAO() {
        return (UsuariosDAO) instantiateDAO(UsuariosDAOHibernate.class);
    }
}
